package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.bo.WordManagerBO;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import form.WordManagerForm;

public class AddWordAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		WordManagerForm addWordForm = (WordManagerForm) form;
		WordManagerBO wordManagerBO = new WordManagerBO();
		String result = null;
		String langSource = addWordForm.getLangSource();
		String langTarget = addWordForm.getLangTarget();
		String word = addWordForm.getWord();
		String meaning = addWordForm.getMean();
		String img = addWordForm.getImg();

		if (!("".equals(word) || word != null)) {
			result = "NOK";
		} else if (wordManagerBO.CheckWord(langSource, langTarget, word)) {
			wordManagerBO.EditWord(langSource, langTarget, word, meaning);
			result = "OK1";
		} else {
			wordManagerBO.AddWord(langSource, langTarget, word, meaning, img);
			result = "OK2";
		}
		return mapping.findForward(result);
	}
}
